package net.lahuobao.export.service.impl;

import java.io.IOException;
import java.io.Writer;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import net.lahuobao.export.service.comm.IHelp;
import net.lahuobao.export.service.stream.ITxtGen;
/**
 * 生成文本文件类
 * @author Administrator
 *
 */
@Service("txtGenService")
public class TxtGenImpl implements ITxtGen {
	@Autowired
	private IHelp helpService;
	/**
	 * 直接txt文件的方式
	 * @param writer 字符输出流
	 * @param response
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 */
	public <T>boolean createStream(Writer writer, HttpServletResponse response,List<T> dataList,int limit,int index) throws IOException, InstantiationException, IllegalAccessException {
	      response.setContentType("application/text;charset=GBK");
	      int len=dataList.size();
	      
	      if(len>0){
	    	  //表头数据,bug:会多次生成表头
	    	  if(index==0){
	    		  String header=helpService.CreateTableHeader(dataList.get(0));
			      writer.write(header);
			      writer.write("\r\n"); 
	    	  }
		      //表内容数据
		      for(int i=0;i<len;i++){
		    	 String item=helpService.CreateTableItem(dataList.get(i));
			      writer.write(item);
			      writer.write("\r\n");
		      }
	      }
	      //limit : 判断分页上限
	      //判断记录数据是否有limit条
	      if(len<limit){
	    	  writer.flush();
	    	  return false;
	      }else{
	    	  return true;
	      }
	}
	

}
